
function plugin0() {
    return document.getElementById('plugin0');
}

plugin = plugin0;
function addEvent(obj, name, func) {
    if (obj.attachEvent) {
        obj.attachEvent("on" + name, func);
    } else {
        obj.addEventListener(name, func, false);
    }
}

function start() {
    document.cookie = "test=123;path=/";
    var test = -1;
    log(plugin().valid ? "有效.." : "无效");
    if (plugin().valid) {
        log("正在初始化...");
        plugin().SetDefaultDev(0);
        test = plugin().StartPreview();
        log("初始化" + (test != 1 ? "<span style='color:red'>失败</span>！" : "成功！"));
    } else {
        log("请检查是否安装了该浏览器插件！");
    }
    if (test == 1) {
        log("初始化摄像头参数...");
        invalidProperty();
        log("摄像头参数设置成功...");
    }
}

function invalidProperty() {
    plugin().SetResolution(0);// 设置分辨率为2592*1944
    plugin().SetScanSize(1);// 设置扫瞄尺寸A3
    plugin().SetVideoRotate(0);// 设置旋转角度0
    plugin().SetJpgQuality(50);// 设置JPG质量50
    plugin().SetRotateCrop(1);// 设置自动纠偏裁边
}

var imgPath;
function doPhoto() {
    var dateStr = new Date().getTime();
    imgPath = "d:\\s600.jpg";
    var isOk = plugin().Scan(imgPath);
    if (!isOk) {
        showPhoto();
        eloamImg().AddImage(imgPath);
    } else
        log('拍照失败！');
}

/**
 * 上传图片和后续操作
 */
function upload() {
    var flag = validate();
    if (!flag)
        return;
    var key = "upload_pic_btn";
    disabledBtn(key);
    var sUrl = rootPath + "/doc/upload_order_doc.do?" + getParams();
    var isOk = plugin().UploadFileByHttp(sUrl, imgPath, "");
    if (isOk) {
        onCancel();
        Iw.Util.successNoty("上传成功！");
        refreshList();
    } else {
        Iw.Util.errorNoty("上传失败！");
    }
    removeDisabledBtn(key);
}

function log(msg) {
    var html = $("#log").html();
    var array = html.split("<br>");
    if (array.length > 5) {
        var att0 = array[0];
        html = html.substring(att0.length + "<br>".length);
    }
    html += msg + "<br/>";
    $("#log").html(html);
}

function eloamImg() {
    return document.getElementById('eloamImg');
}

function CloseDevice() {
    plugin().CloseDevice();
}

// 上传弹出层
function showPhoto() {
    scrollEasyuiVisible("photo_win");
}

function scrollEasyuiVisible(id) {
    $("#" + id).window({
        closed: false,
        modal: true,
        onOpen: function () {
            $(document.body).css({
                "overflow-x": "hidden",
                "overflow-y": "hidden"
            });
            return true;
        },
        onClose: function () {
            $(document.body).css({
                "overflow-x": "auto",
                "overflow-y": "auto"
            });
            return true;
        }
    });
    $('#' + id).window('move', {
        top: $(document).scrollTop() + 100
    });
}

function closeEasyuiWin(id) {
    $("#" + id).window("close");
}
function onCancel() {
    closeEasyuiWin("photo_win");
}

function fn_openScaner() {
    $("#openScaner").hide();
    $("#closeScaner").show();
    $("#scanContainer").show();
    start();
    $('body,html').animate({scrollTop: document.body.scrollHeight}, 1000);
}

function fn_closeScaner() {
    $("#openScaner").show();
    $("#closeScaner").hide();
    CloseDevice();
    $("#scanContainer").hide();
}

